Get premium membership and access questions with answers, video lessons as well as revision papers.
As the program is written, when an object is passed to getval(), a bitwise copy is made. When getval() returns and that copy is destroyed, the memory allocated to that object (which is pointed to by p) is released. However, this is the same memory still required by the object used in the call to getval(). The correct version is;
// This program is now fixed.
#include
#include
using namespace std;
class myclass{
int *p;
public:
myclass(int i);
myclass(const myclass &o); // Copy constructor
~myclass() {delete p;}
friend int getval(myclass o);
};
myclass::myclass(int i)
{
p = new int;
if(!p) {
cout << "Allocatior\n";
exit(1);
}
*p = i;
}
// Copy constructor
myclass::myclass(const myclass &o)
{
p = new int; // allocate copy's own memory
if(!p) {
cout llocation error\n";
exit(1);
}
*p = *o.p;
}
int getval(myclass o)
{
return *o.p; // get value
}
int main ()
{
myclass a(1), b(2);
cout <cout << "\n";
cout << getval(a) << " " << getval(b);
return 0;
Githiari answered the question on May 29, 2018 at 17:49
- Imagine a situation in which two classes, called pr1 and pr2, shown below, share one printer.Further imagine that other parts of your program need to...(Solved)
Imagine a situation in which two classes, called pr1 and pr2, shown below, share one printer.Further imagine that other parts of your program need to when the printer is in use by an object of either of these two classes. Create a function in C++ called inuse() that returns true when the printer is being used by either and false otherwise. Make this function a friend of both pr1 and pr2.
class pri1 {
int printing; //...
public:
pr1 () {printing = 0}
void set_print (int status) {printing = status;}
// ...
};
class pr2 {
int printing;
// ...
public:
pr2 () {printing = 0;}
void set_prt status) {printing = status; }
// ...
};
Date posted: May 29, 2018. Answers (1)
- Given this class;
class planet {
int moons;
double dist_from_sun; // in miles
double diameter;
double mass;
public:
//...
double get_ () { return disfrom_sun; }
};
Create a function called light() using c++...(Solved)
Given this class;
class planet {
int moons;
double dist_from_sun; // in miles
double diameter;
double mass;
public:
//...
double get_ () { return disfrom_sun; }
};
Create a function called light() using c++ language that takes as an argument an object of type planet and returns the number of secos that it takes light from the sun to reach the planet.(Assume that light travels at 186,000 miles per second and that dist_from_sun is specified in miles.)
Date posted: May 29, 2018. Answers (1)
- When an object of a derived class is assigned to another object of the same derived class, is the data associated with the base class...(Solved)
When an object of a derived class is assigned to another object of the same derived class, is the data associated with the base class copied? To find out, use the following two classes and write a program that demonstrates what happens.
class base {
int a;
public:
void load_a(int n) { a= n; }
int ge() { return a;}
};
class derived : public base {
int b;
public:
void load_b(int n) {b=n;}
int get_b() {return b;}
};
Date posted: May 29, 2018. Answers (1)
- Using the stack class, write a function called loadstack() that returns a stack that is already loaded with letters of the alphabet (a-z.). Assign this...(Solved)
Using the stack class, write a function called loadstack() that returns a stack that is already loaded with letters of the alphabet (a-z.). Assign this stack to another object in the calling routine and prove that it contains the alphabet. Be sure to change the stack size so it is large enough to hold the alphabet.
Date posted: May 29, 2018. Answers (1)
- Explain five good programming practices(Solved)
Explain five good programming practices.
Date posted: May 29, 2018. Answers (1)
- List five examples of softwares(Solved)
This is a question based on Introduction to Computer Systems unit in Computer science course
Date posted: May 26, 2018. Answers (1)
- List three categories of operating systems(Solved)
List three categories of operating systems.
Date posted: May 26, 2018. Answers (1)
- As a programmer, what is the significance of the Integrated Development Environment(IDE) in programming?(Solved)
As a programmer, what is the significance of the Integrated Development Environment(IDE) in programming?
Date posted: May 26, 2018. Answers (1)
- As a programmer, briefly explain why comments are used when writing a program(Solved)
As a programmer, briefly explain why comments are used when writing a program.
Date posted: May 26, 2018. Answers (1)
- Define the following terms as used in computer programming: (a.) A compiler (b.) A debugger (c.) An algorithm(Solved)
Define the following terms as used in computer programming: (a.) A compiler (b.) A debugger (c.) An algorithm
Date posted: May 26, 2018. Answers (1)
- What is computer programming?(Solved)
What is computer programming?
Date posted: May 26, 2018. Answers (1)
- Using an example, differentiate a code from a program(Solved)
Using an example, differentiate a code from a program.
Date posted: May 26, 2018. Answers (1)
- Explain three techniques of detecting deadlocks in a computer system(Solved)
Explain three techniques of detecting deadlocks in a computer system.
Date posted: May 16, 2018. Answers (1)
- Explain common types of computer networks(Solved)
Explain common types of computer networks.
Date posted: May 16, 2018. Answers (1)
- What is social Engineering?(Solved)
What is social Engineering?
Date posted: May 16, 2018. Answers (1)
- Explain characteristics of information security within an organization? (Solved)
Explain characteristics of information security within an organization?
Date posted: May 16, 2018. Answers (1)
- Giving examples explain threats to data security(Solved)
Giving examples explain threats to data security.
Date posted: May 16, 2018. Answers (1)
- What is a cyber crime?(Solved)
What is a cyber crime?
Date posted: May 16, 2018. Answers (1)
- Describe the difference between an internal and an external hyperlink(Solved)
Describe the difference between an internal and an external hyperlink
Date posted: May 14, 2018. Answers (1)
- What’s the difference between encoding, encryption, and hashing?
(Solved)
What’s the difference between encoding, encryption, and hashing?
Date posted: May 13, 2018. Answers (1)